package com.awspaas.user.apps.cpq.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.BOCopyAPI;
import com.actionsoft.sdk.local.api.BOQueryAPI;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ibm.db2.jcc.am.uc;

public class CPQ_GetNewProjectMsgWeb {

	public String getNewProjectMsg(UserContext userContext, String datas) throws Exception {
		try {
			ResponseObject ro = ResponseObject.newOkResponse();
			Map<String, String> map=new HashMap<String, String>();
		    JSONObject json=JSONObject.parseObject(datas);
		    String sbcs=json.getString("equipment");
		    String azcs=json.getString("install");
			 ObjectMapper objectMapper = new ObjectMapper();
			 List<String> list = objectMapper.readValue(sbcs, new TypeReference<List<String>>() {});
			 String xmlx=list.get(0);
			 String sbxh=list.get(2);
			 String dzsm=list.get(3);
			BO pData= SDK.getBOAPI().query("BO_EU_CPQ_TEMPLATE_PRICE").addQuery("XMLX=", xmlx).addQuery("CPXH=", sbxh).addQuery("DZSM=", dzsm).detail();
			List<String> azlist = objectMapper.readValue(azcs, new TypeReference<List<String>>() {});
			 String azxmlx=azlist.get(0);
			 String azsbxh=azlist.get(2);
			BO ifData= SDK.getBOAPI().query("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE").addQuery("XMLX=", azxmlx).addQuery("CPXH=", azsbxh).detail(); 
			if(null==pData) {
				map.put("type", "0");
				map.put("msg", "所选择的项目类型模板不存在请重新选择");
				ro.setData(map);
				return ro.toString();
			}
			if(null==ifData) {
				map.put("type", "0");
				map.put("msg", "所选择的项目类型的安装费模板不存在请重新选择");
				ro.setData(map);
				return ro.toString();
			}
			//将查的数据带入项目报价申请单中
			BO bo=new BO();
			bo.set("XMLX", xmlx);
			bo.set("CPXH", sbxh);
			bo.set("DZSM", dzsm);
			String  SXXT=pData.getString("SXXT");
			String  SXXTBH=pData.getString("SXXTBH");
			String  SYPZ=pData.getString("SYPZ");
			String  CLF=pData.getString("CLF");
			String  RGF=pData.getString("RGF");
			String  WXWG=pData.getString("WXWG");
			String  CGF=pData.getString("CGF");
			String  XMCBYS=pData.getString("XMCBYS");
			String  SBAZFCB=ifData.getString("SBAZFCB");
			String  AZFZJ=ifData.getString("AZFZJ");
			bo.set("SXXT", SXXT);
			bo.set("SXXTBH",SXXTBH );
			bo.set("SYPZ", SYPZ);
			bo.set("CLF", CLF);
			bo.set("RGF",RGF );
			bo.set("WXWG", WXWG);
			bo.set("CGF", CGF);
			bo.set("XMCBYS", XMCBYS);
			bo.set("AZFZJ",AZFZJ );
			bo.set("SBAZFCB",SBAZFCB );
			String pId=pData.getBindId();
			String ifId=ifData.getBindId();
			//参数信息、设备明细、设备安装明细、安装费用明细
			BOQueryAPI gridsQuery=SDK.getBOAPI().query("BO_EU_CPQ_TEMPLATE_PRICE_MODULE").bindId(pId);
			BOQueryAPI parameterQuery=SDK.getBOAPI().query("BO_EU_CPQ_TEMPLATE_PARAMETER").bindId(pId);
			BOQueryAPI ifQuery=SDK.getBOAPI().query("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_GRID").bindId(ifId);
			BOQueryAPI priceQuery=SDK.getBOAPI().query("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_PRICE").bindId(ifId);
			//创建项目报价记录台账实例
			String pid=SDK.getProcessAPI().createBOProcessInstance("obj_2ce868263b6943c59628031d9943a7e9",userContext.getUID(), "").getId();
            SDK.getBOAPI().create("BO_EU_CPQ_PROJECT_MSG", bo, pid, userContext.getUID());
			BOCopyAPI newGrids=gridsQuery.copyTo("BO_EU_CPQ_PROJECT_MSG_GRIDS", pid);
			newGrids.exec();
			BOCopyAPI newParameter=parameterQuery.copyTo("BO_EU_CPQ_PROJECT_MSG_PARAMETER_GRIDS", pid);
			newParameter.exec();
			BOCopyAPI newIf=ifQuery.copyTo("BO_EU_CPQ_PROJECT_MSG_IF", pid);
			newIf.exec();
			BOCopyAPI newPrice=priceQuery.copyTo("BO_EU_CPQ_PROJECT_MSG_IFP", pid);
			newPrice.exec();
			map.put("type", "1");
			map.put("bindId", pid);
			ro.setData(map);
			return ro.toString();
		} catch (Exception e) {
		   throw e;
		}
		
	}

}
